
<template>
  <div style="height: 100%;display: flex;flex-direction: column;overflow: hidden" id="root1">
    <div>
      <el-card style="width: 100%;">
        角色权限管理
        <span style="float: right">
            <el-button @click="()=>{refresh()}" type="primary">刷新</el-button>
            <el-button @click="()=>{$router.push('/permission/role')}">回到列表页</el-button>
        </span>
      </el-card>
    </div>
    <div style="margin-top: 10px;flex: 1;display:flex;flex-direction: column;overflow: auto" >
      <el-card style="" :body-style="state.cartBodyStyle" >
        <template #header>
          <div class="card-header">
            <el-text class="mx-1" type="primary" size="large"  style="font-weight: bold">已分配权限</el-text>
            <el-button class="button"   type="warning" size="large" @click="toUnAssign">撤销权限</el-button>
          </div>
        </template>
      </el-card>
      <el-table
          :data="state.rolePermissionModel.permissionAssigned"
          border
          ref="tableRefAssigned"
          v-loading="state.loading"
          element-loading-text="正在加载..."
          :header-cell-style="{'text-align':'center','font-weight':'bold'}"
          :cell-style="{'text-align':'center'}"
          row-key="id"
          :tree-props="{ children: 'children'}"
          @selection-change="onSelectAssigned"
          style="flex: 1;">
        <el-table-column type="selection"></el-table-column>
        <template v-for="item in columns">
          <el-table-column :prop="item.prop" :label="item.label" :fixed="item.fixed" :min-width="item.minWidth" :sortable="item.sortable">
            <template #default="scope" v-if="item.render">
              <render-column :render="item.render" :scope="scope" :item="item"></render-column>
            </template>
          </el-table-column>
        </template>
      </el-table>
      <el-card style="" :body-style="state.cartBodyStyle">
        <template #header>
          <div class="card-header">
            <el-text class="mx-1" type="warning" size="large" style="font-weight: bold">未分配权限</el-text>
            <el-button class="button"   type="primary" size="large" @click="toAssign">分配权限</el-button>
          </div>
        </template>
      </el-card>
      <el-table
          :data="state.rolePermissionModel.permissionUnAssigned"
          border
          ref="tableRefUnAssigned"
          v-loading="state.loading"
          element-loading-text="正在加载..."
          :header-cell-style="{'text-align':'center','font-weight':'bold'}"
          :cell-style="{'text-align':'center'}"
          row-key="id"
          :tree-props="{ children: 'children'}"
          @selection-change="onSelectUnAssigned"
          style="flex: 1;">
        <el-table-column type="selection"></el-table-column>
        <template v-for="item in columns">
          <el-table-column :prop="item.prop" :label="item.label" :fixed="item.fixed" :min-width="item.minWidth" :sortable="item.sortable">
            <template #default="scope" v-if="item.render">
              <render-column :render="item.render" :scope="scope" :item="item"></render-column>
            </template>
          </el-table-column>
        </template>
      </el-table>
    </div>

    <guigu-loading :is-loading="state.loading" text="正在添加，请稍等..."/>
  </div>
</template>

<script setup>
import GuiguLoading from "@/components/GuiguLoading/index.vue";
import {onMounted, reactive, ref} from "vue";
import {CirclePlus,Delete} from "@element-plus/icons-vue";
import {createPermission} from "@/api/backend/permission/permission.js";
import {ElMessage} from "element-plus";
import {useRoute, useRouter} from "vue-router";
import {assignPermissions, rolePermission, unassignPermissions} from "@/api/backend/permission/role_permission.js";
import RenderColumn from "@/components/RenderColumn/index.vue";
const $route=useRoute()
const $router=useRouter()
const state=reactive({
  loading:false,
  cartBodyStyle:{},
  rolePermissionModel:{
    roleId:$route.params.roleId,
    routeAssigned:[],
    routeUnAssigned:[],
    permissionAssigned:[],
    permissionUnAssigned:[]
  },
  rolePermissionAssign:{
    roleId:$route.params.roleId,
    permissionIds:[]
  },
  rolePermissionUnAssign:{
    roleId:$route.params.roleId,
    permissionIds:[]
  }
})
const tableRefUnAssigned=ref()
const tableRefAssigned=ref()
const onSelectUnAssigned=(selection)=>{
  state.rolePermissionAssign.permissionIds=selection.map(item=>item.permissionId)
}

const toUnAssign=()=>{
  if(state.rolePermissionUnAssign.permissionIds.length===0)return
  unassignPermissions(state.rolePermissionUnAssign)
      .then(res=>{

      }).finally(()=>{
        refresh()
  })
}
const toAssign=()=>{
  if(state.rolePermissionAssign.permissionIds.length===0)return
  assignPermissions(state.rolePermissionAssign)
      .then(res=>{
      }).finally(()=>{
        refresh()
  })
}

const onSelectAssigned=(selection)=>{
  state.rolePermissionUnAssign.permissionIds=selection.map(item=>item.permissionId)
}
const columns = [
  {
    prop: "permissionId",
    label: "权限id",
    minWidth:'160px'
  },

  {
    prop: "permissionCode",
    label: "权限编码",
    minWidth:'160px',
  },
  {
    prop: "permissionName",
    label: "权限名称",
    minWidth:'160px',
  },
  {
    prop: "desc",
    label: "描述",
    minWidth:'160px'
  },
  {
    prop: "path",
    label: "路径",
    minWidth:'160px',
  },
  {
    prop: "createTime",
    label: "创建时间",
    minWidth:'160px',
  },
  {
    prop: "updateTime",
    label: "更新时间",
    minWidth:'160px',
  }

]
const submitForm= formRef=>{
}

const resetForm= formRef=>{

}


const refresh=()=>{
  rolePermission($route.params.roleId)
      .then(res=>{
        state.rolePermissionModel=res.data
      })
}

onMounted(()=>{
  refresh()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.box{
  flex: 1;
  display: flex;
  flex-direction: column;
}
</style>